package util.content;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import replaceWords.Complex;
import replaceWords.MarkDealXML;

/*这个类的作用：拿出xml中正则抓取需要的参数，把网页的文件bodyString和正则规则传给ContentCapture类，并调用ContentCapture从ContentCapture中获得抓取结果*/

public class ContentRex {

	List sourceSiteList = null;

	public String getTitle() {
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			String titleRex = sourceSite.getChildText("titleRex");
			Pattern p = Pattern.compile(titleRex);
			Matcher m = p.matcher(title);
			if (m.find()) {
				title = m.group(1).trim();
			}
		}
		return title;
	}

	String contentString = "";

	public String getContentString() {
		contentString = "";
		ContentCapture cc;
		Complex wordToWords = new Complex();
		MarkDealXML mdx = new MarkDealXML();
		mdx.setMarkList(markList);
		mdx.setMarkSuccedaneumsList(markSuccedaneumsList);
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			
				List spiderSloveList = sourceSite.getChildren("spiderSlove");
				for (Iterator iterSpider = spiderSloveList.iterator(); iterSpider.hasNext();) {
					Element spider = (Element) iterSpider.next();
					List contentList = spider.getChildren("content");
					for (Iterator iterContent = contentList.iterator(); iterContent.hasNext();) {
						Element contentE = (Element) iterContent.next();
						// 初始化乱序类rc和正则抓取类cc
						cc = new ContentCapture();
						// 拿出xml文件中的参数
						String contentArea = contentE.getChildTextTrim("contentArea");
						String oneContentArea = contentE.getChildTextTrim("oneContentArea");
						String mainArea = contentE.getChildTextTrim("mainArea");
						String reAskArea = contentE.getChildTextTrim("reAskArea");
						String reAskRex = contentE.getChildTextTrim("reAskRex");
						String reAskRexTagQ = contentE.getChildTextTrim("reAskRexTagQ");
						String reAskRexTagA = contentE.getChildTextTrim("reAskRexTagA");
						String contentdel = contentE.getChildTextTrim("contentdel");
						String contentdelAfterSpider = contentE.getChildTextTrim("contentdel2");
						String mainRex = contentE.getChildTextTrim("mainRex");
						String contentAttri = contentE.getAttributeValue("id");
						// 用contentArea截取出整个问题、最佳或普通回答的区块
						cc.setContentdelAfterSpider(contentdelAfterSpider);
						cc.setContentArea(contentArea);
						cc.setContentdel(contentdel);
						cc.setReAskArea(reAskArea);
						cc.setMainArea(mainArea);
						cc.setOneContentArea(oneContentArea);
						cc.setMainRex(mainRex);
						cc.setReAskRex(reAskRex);
						cc.setReAskRexTagA(reAskRexTagA);
						cc.setReAskRexTagQ(reAskRexTagQ);
						cc.setContentAttri(contentAttri);
						contentString = contentString + cc.getContent(bodyString);
						cc = null;
					}
				}
			
			// 过滤每行开始和结束后的空格
			ArrayList<String> linelineString = new ArrayList<String>();
			Pattern onelinePattern = Pattern.compile(".+");
			Matcher onelineMatcher = onelinePattern.matcher(contentString);
			while (onelineMatcher.find()) {
				linelineString.add(onelineMatcher.group().trim());
			}

			contentString = "";
			for (int i = 0; i < linelineString.size(); i++) {

				contentString = contentString + linelineString.get(i) + "\n";
			}
			// 识别中英文行
			for (int lineId = 0; lineId < linelineString.size(); lineId++) {
				String oneLineTxt = linelineString.get(lineId).trim();
				if (oneLineTxt.startsWith("<") && oneLineTxt.endsWith(">")) {
					continue;
				} else {
					ChineseWordsCounter cwc = new ChineseWordsCounter();
					Pattern chineseWordPattern = Pattern.compile("[\u4e00-\u9fa5]");
					Matcher chineseWordmatcher = chineseWordPattern.matcher(oneLineTxt);
					if (chineseWordmatcher.find() && cwc.count(oneLineTxt)) {
						oneLineTxt = mdx.markdel(oneLineTxt);
						try {
							oneLineTxt = wordToWords.segWords(oneLineTxt, " ");
							oneLineTxt = mdx.markAdd(oneLineTxt);
							linelineString.set(lineId, oneLineTxt);
						} catch (IOException e) {

							e.printStackTrace();
						}

					} else {
						// System.out.println(oneLineTxt);
						Pattern pBags = Pattern.compile("[a-zA-Z]{2,100}");
						Matcher mBags = pBags.matcher(oneLineTxt);
						while (mBags.find()) {
							String s = mBags.group();
							oneLineTxt = oneLineTxt.replaceAll(s, "<zf" + s + "zf>");
						}

						oneLineTxt = oneLineTxt.replaceAll("\\<[zf<]*zf", "<zf").replaceAll("[zf>]*\\>", "zf>");
						linelineString.set(lineId, oneLineTxt.trim());
					}
				}
			}
			// contentString清除原来的内容装入去掉空格的内容
			contentStringToTranslate = "";
			for (int i = 0; i < linelineString.size(); i++) {
				contentStringToTranslate = contentStringToTranslate + linelineString.get(i) + "\n";
			}
		}
		return contentString;
	}

	String bodyString = null;

	public void setBodyString(String bodyString) {
		this.bodyString = bodyString;
	}

	String title = null;

	public void setTitle(String title) {
		this.title = title;
	}

	String contentStringToTranslate = null;

	public String getContentStringToTranslate() {
		return contentStringToTranslate;
	}

	List<String> markList = new ArrayList<String>();
	List<String> markSuccedaneumsList = new ArrayList<String>();

	ContentRex() {

		SAXBuilder builder = new SAXBuilder(false);
		try {
			String xmlpath = "spiderParameter.xml";
			Document doc = builder.build(xmlpath);
			Element sourceSite = doc.getRootElement();
			sourceSiteList = sourceSite.getChildren("sourceSite");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			Document doc = builder.build("D:\\MyTool\\markList.xml");
			Element wordsElement = doc.getRootElement();
			List rowslist = wordsElement.getChildren("row");
			for (Iterator iter = rowslist.iterator(); iter.hasNext();) {
				Element row = (Element) iter.next();
				List fieldList = row.getChildren("field");
				for (Iterator iterField = fieldList.iterator(); iterField.hasNext();) {
					Element field = (Element) iterField.next();
					String fieldName = field.getAttributeValue("name");
					if (fieldName.equalsIgnoreCase("mark")) {
						String word = field.getText();
						markList.add(word);
					} else {
						String newone = field.getText();
						markSuccedaneumsList.add(newone);
					}
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
